﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Data;
using OgilvyOne.Utility;

namespace OgilvyOne.BLL
{
    /// <summary>
    /// 列表标签,循环替换标签内容
    /// <cms:list value="mode方法名,返回DataSet" rows="行数">
    ///     循环列表数据<cms value="DataSet字段名"/>
    /// </cms:list>
    /// </summary>
    class ListLabel : CommonLabel
    {
        public ListLabel(CommonModel model)
            : base(model)
        {

        }

        private int rows;
        public int Rows
        {
            get { return rows; }
            set { rows = value; }
        }

        public override object[] GetMoedthParm()
        {
            if (rows > 0) return new object[] { rows };
            return null;
        }
        
        public override string GetLabelHtml()
        {
            StringBuilder sb = new StringBuilder("");
            DataSet dataset = (DataSet)Execute();
            DataTable table = dataset.Tables[0];
            MatchCollection mc = regex.Matches(LabelNodeStr);
            for (int i = 0; i < table.Rows.Count; i++)
            {
                DataRow row = table.Rows[i];
                string lihtml = LabelNodeStr;
                foreach (Match m in mc)
                {
                    string key = GetCmsValueStr(m.Value);//.Replace("<cms", "").Replace("value=", "").Replace("'", "").Replace("\"", "").Trim();
                    lihtml = StringUtil.ReplaceStr(lihtml, m.Value, row[key].ToString());
                }
                sb.AppendLine(lihtml);
            }
            return sb.ToString();
        }
    }
}
